package com.jinduplan.planmanager.ui.activity.project

import android.text.Editable
import android.util.Log
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.cxz.kotlin.baselibs.adapter.CustomRecyclerAdapter
import com.cxz.kotlin.baselibs.adapter.ViewHolder
import com.cxz.kotlin.baselibs.base.BaseMvpActivity
import com.cxz.kotlin.baselibs.utils.GsonHelper
import com.jinduplan.planmanager.R
import com.jinduplan.planmanager.bean.ProjectSignBean
import com.jinduplan.planmanager.bean.ProjectUploadSuccessBean
import com.jinduplan.planmanager.bean.RequestPutProjectBean
import com.jinduplan.planmanager.bean.SignSuccess
import com.jinduplan.planmanager.mvp.contract.ProjectUploadSignContract
import com.jinduplan.planmanager.mvp.presenter.ProjectUploadSignPresenter
import com.jinduplan.planmanager.utils.CustomRecyclerFilterableAdapter
import com.jinduplan.planmanager.utils.ResUtil
import com.jinduplan.planmanager.widgets.ComfirCancelDialog
import com.jinduplan.planmanager.widgets.datepicker.DateFormatUtils
import kotlinx.android.synthetic.main.activity_project.back
import kotlinx.android.synthetic.main.activity_project.recyclerView
import kotlinx.android.synthetic.main.activity_project_upload_detail.*
import kotlinx.android.synthetic.main.activity_project_upload_sign.*


class ProjectUploadSignActivity :
    BaseMvpActivity<ProjectUploadSignContract.View, ProjectUploadSignContract.Presenter>(),
    ProjectUploadSignContract.View {

    private var projectUploadData: ArrayList<ProjectUploadSuccessBean>? = null
    private var daily_construction_id: Int = 0
    private var signature_date: String = "0"
    private var mPosition: Int = 0

    override fun createPresenter(): ProjectUploadSignContract.Presenter =
        ProjectUploadSignPresenter()

    override fun start() {
        projectUploadData = ArrayList();
        projectUploadData?.addAll(intent.getParcelableArrayListExtra("project_upload_data"))
        if (projectUploadData!!.size > 0) {
            projectUploadData!![0].isSelect = true
            daily_construction_id = (projectUploadData!![0].id).toInt()
            tv_type_num.text = projectUploadData!![0].construction_process
            tv_area_num.text = "${projectUploadData!![0].completion_area.toString()}m²"
        }

        recyclerView.adapter = object : CustomRecyclerFilterableAdapter<ProjectUploadSuccessBean>(
            this, R.layout.item_project_sign, projectUploadData
        ) {
            override fun convert(holder: ViewHolder?, t: ProjectUploadSuccessBean?, position: Int) {
                var textName = holder?.getView<TextView>(R.id.tv_name)
                textName?.text = t?.full_name
                textName?.isSelected = t?.isSelect == true

                holder?.convertView?.setOnClickListener {
                    var projects = ArrayList<Any>()
                    projects.addAll((recyclerView.adapter as CustomRecyclerFilterableAdapter<Any>).data)
                    for (index in 0 until projects!!.size) {
                        (projects!![index] as ProjectUploadSuccessBean).isSelect = false
                    }
                    (projects!![position] as ProjectUploadSuccessBean).isSelect = true
                    recyclerView.adapter?.notifyDataSetChanged()
                    tv_type_num.text = t?.construction_process
                    tv_area_num.text = t?.completion_area.toString()
                    view_signature.clear()
                    daily_construction_id = ((projects!![position] as ProjectUploadSuccessBean).id).toInt()
                    mPosition = position
                }
            }

        }
        recyclerView.layoutManager = LinearLayoutManager(this)


    }

    override fun initView() {
        super.initView()
        setStatusBarColor(ResUtil.getC(R.color.white))


        back.setOnClickListener {
            ComfirCancelDialog(this).setContent("是否退出？", "退出后您的数据将会清空") {
                finish()
            }.show()

        }
        tv_reSign.setOnClickListener {
            ComfirCancelDialog(this).setContent("是否重新签名？", "确认后签名将会消失") {
                view_signature.clear()
            }.show()
        }
        bt_config.setOnClickListener {
            view_signature.save("/sdcard/sign.png", true, 10)
            mPresenter?.uploadInPic(view_signature.savePath)
        }
        val endTimestamp = System.currentTimeMillis()
        signature_date = DateFormatUtils.long2Str(endTimestamp, false)

        ed_search.addTextChangedListener(object :
            android.text.TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                (recyclerView.adapter as CustomRecyclerFilterableAdapter<Any>).filter.filter(s.toString())
            }

            override fun afterTextChanged(s: Editable?) {
            }
        })


    }

    override fun attachLayoutRes(): Int {
        return R.layout.activity_project_upload_sign
    }

    override fun uploadPicSuccess(picUrl: String) {
        mPresenter?.sign(picUrl, daily_construction_id.toString(), signature_date)
    }

    override fun projectSignSuccess(bean: SignSuccess) {
//        var projects = ArrayList<ProjectUploadSuccessBean>()
//        projects.addAll((recyclerView.adapter as CustomRecyclerFilterableAdapter<ProjectUploadSuccessBean>).data)
        if (projectUploadData!!.size > 0) {
            if (projectUploadData!!.size == 1) {
                finish()
            } else {
                projectUploadData!!.removeAt(mPosition)
                (projectUploadData!![0] as ProjectUploadSuccessBean).isSelect = true
                daily_construction_id = ((projectUploadData!![0] as ProjectUploadSuccessBean).id).toInt()
                tv_type_num.text = (projectUploadData!![0] as ProjectUploadSuccessBean).construction_process
                tv_area_num.text = (projectUploadData!![0] as ProjectUploadSuccessBean).completion_area.toString()
                view_signature.clear()
                recyclerView.adapter?.notifyDataSetChanged()
            }

        }
    }
}